/**
 * 
 */
package de.nak.fireDepartment.administrationAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.web.struts.ActionSupport;
import de.nak.fireDepartment.actionForms.EquipmentForm;
import de.nak.fireDepartment.exceptions.EquipmentAlreadyExistsException;
import de.nak.fireDepartment.exceptions.ImageUploadException;
import de.nak.fireDepartment.model.Equipment;
import de.nak.fireDepartment.service.EquipmentService;
import de.nak.fireDepartment.service.ImageUploadService;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action class for creating and edit a department.
 * @author Nils Bredow
 */
public class CreateEditEquipmentAction extends ActionSupport {
	
	/** 
	 * This method creates or updates a car
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ActionForm => EquipmentForm
		EquipmentForm equipmentForm = (EquipmentForm) form;
		// Get the equipment from equipmentform
		Equipment equipment = equipmentForm.getEquipment();
		// Clear ErrorCode to display
		equipmentForm.setErrorCode("");
		
		// get the equipment and image service
		EquipmentService equipmentService = (EquipmentService)getWebApplicationContext().getBean("equipmentService");
		ImageUploadService imageUploadService = (ImageUploadService)getWebApplicationContext().getBean("imageUploadService");
		
		// validate the user-login
		LoginService loginService = (LoginService)getWebApplicationContext().getBean("loginService");
		if(!loginService.checkUserIsValidated()){
			return mapping.findForward("missinglogin");
		}
		
		// get the formfile
        FormFile aFile = equipmentForm.getaEquipmentImageFile();
        String filePath = "";
        // if there are errors while uploading, the message should be put into the errorcode
        try{
        	filePath = imageUploadService.uploadFile(aFile, ImageUploadService.EQUIPMENT, getServletContext());
        }catch(ImageUploadException ex){
        	equipmentForm.setErrorCode(ex.getMessage());
        }
        
        // if the car id is set, you have to update the car, otherwise create a new car.
        try{
        	if(equipment.getEquipmentId() > 0){
        		equipmentService.updateEquipment(equipment.getEquipmentId(), equipment.getModelName(), equipment.getYearOfManufacture(), equipment.getManufactureCompany(), equipment.getDescriptionText(),filePath);
    		}else{
    			equipmentService.createEquipment(equipment.getModelName(), equipment.getYearOfManufacture(), equipment.getManufactureCompany(), equipment.getDescriptionText(),filePath);
    		}
        }catch(EquipmentAlreadyExistsException ex){
        	equipmentForm.setErrorCode(ex.getMessage());
        }
			
		// Reload list
        equipmentForm.setSelectedKey(null);
		equipmentForm.setEquipments(equipmentService.listEquipments());
		// Return result
		return mapping.findForward("success");
	}
}
